package com.bracelet.util.netty;

import cn.hutool.core.util.StrUtil;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
//NettySession和NettySessionManager类用于管理客户端会话
@Slf4j
public class NettySession {
    /**
     *channel   id
     */
    private String channelId;

    /**
     * 业务关联的key
     */
    private String bizKey;

    /**
     * 真正的连接对象
     */
    private Channel channel = null;

    /**
     * 连接上下文
     */
    private ChannelHandlerContext ctx = null;

    private LocalDateTime createTime;

    private LocalDateTime lastTime;

    /**
     * 消息流水号 word(2字节) 按发送顺序从 0 开始循环累加
     */
    private int currentFlowId = 0;


    public static NettySession build(String bizKey, ChannelHandlerContext ctx) {
        NettySession session = new NettySession();
        Channel channel = ctx.channel();
        session.bizKey = bizKey;
        session.channelId = channel.id().asLongText();
        session.ctx = ctx;
        session.channel = channel;
        session.createTime = LocalDateTime.now();
        session.lastTime = session.createTime;
        return session;
    }

    public boolean isActive() {
        if (ctx == null) {
            return false;
        }

        if (channel == null) {
            return false;
        }

        return channel.isActive();
    }

    public boolean isWritable() {
        if (ctx == null) {
            return false;
        }

        if (channel == null) {
            return false;
        }

        return channel.isWritable();
    }
    public boolean writeAndFlush(byte[] bytes) {
        if (bytes == null) {
            return false;
        }

        if (bytes.length == 0) {
            return false;
        }

        if (!isActive()) {
            return false;
        }

        if (!isWritable()) {
            return false;
        }

        ByteBuf retBuf = channel.alloc().buffer(bytes.length);
        retBuf.writeBytes(bytes);
        channel.writeAndFlush(retBuf);

        return true;
    }

    public boolean writeAndFlush(String msg){
        if(StrUtil.isBlank(msg)) {
              return false;
        }

        if (msg.length() == 0) {
            return false;
        }

        if (!isActive()) {
            return false;
        }

        if (!isWritable()) {
            return false;
        }
        channel.writeAndFlush(msg);
        return true;
    }

    /**
     * 关闭连接
     */
    public void close() {
        try {
            ctx.close();
        } catch (Exception ex) {
            log.error(ex.getLocalizedMessage(), ex);
        }
    }

    public String getChannelId() {
        return channelId;
    }

    public void setChannelId(String channelId) {
        this.channelId = channelId;
    }

    public String getBizKey() {
        return bizKey;
    }

    public void setBizKey(String bizKey) {
        this.bizKey = bizKey;
    }

    public Channel getChannel() {
        return channel;
    }

    public void setChannel(Channel channel) {
        this.channel = channel;
    }

    public LocalDateTime getCreateTime() {
        return createTime;
    }

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
    }

    public LocalDateTime getLastTime() {
        return lastTime;
    }

    public void setLastTime(LocalDateTime lastTime) {
        this.lastTime = lastTime;
    }
}
